#include "dd_base.h"
#include "util/mmgr.h"		// Needs to come in early


using namespace dd;

PrimitiveBase::PrimitiveBase()
{

}

PrimitiveBase::~PrimitiveBase()
{

}

void PrimitiveBase::AddVertex(DBG_BUFFER* _pBuffer, const math::Vec3& _From, COLOR _Color)
{
	if (_pBuffer->CurrentVertC + 2 >= _pBuffer->MaxVertC)
	{
		FlushBuffer(_pBuffer);
	}
	_pBuffer->DataA[_pBuffer->CurrentVertC].color = _Color;
	_pBuffer->DataA[_pBuffer->CurrentVertC].x = _From.x;
	_pBuffer->DataA[_pBuffer->CurrentVertC].y = _From.y;
	_pBuffer->DataA[_pBuffer->CurrentVertC++].z = _From.z;
}


void PrimitiveBase::AddLine(DBG_BUFFER* _pBuffer, const math::Vec3& _From, const math::Vec3& _To, COLOR _Color)
{
	if (_pBuffer->CurrentVertC + 2 >= _pBuffer->MaxVertC)
	{
		FlushBuffer(_pBuffer);
	}
	_pBuffer->DataA[_pBuffer->CurrentVertC].color = _Color;
	_pBuffer->DataA[_pBuffer->CurrentVertC].x = _From.x;
	_pBuffer->DataA[_pBuffer->CurrentVertC].y = _From.y;
	_pBuffer->DataA[_pBuffer->CurrentVertC++].z = _From.z;

	_pBuffer->DataA[_pBuffer->CurrentVertC].color = _Color;
	_pBuffer->DataA[_pBuffer->CurrentVertC].x = _To.x;
	_pBuffer->DataA[_pBuffer->CurrentVertC].y = _To.y;
	_pBuffer->DataA[_pBuffer->CurrentVertC++].z = _To.z;
}

void PrimitiveBase::FlushBuffer(DBG_BUFFER* _pBuffer)
{
	if (_pBuffer->CurrentVertC==0)
		return;	// no need to render.. nothing here

	// render
	_pBuffer->pGraphics->DrawVertex3D(_pBuffer->DataA, _pBuffer->CurrentVertC, Graphics::PT_LINE_LIST);

	// reset
    _pBuffer->CurrentVertC = 0;	
}

void PrimitiveBase::FlushBufferTri(DBG_BUFFER* _pBuffer)
{
	if (_pBuffer->CurrentVertC==0)
		return;	// no need to render.. nothing here

	// render
	_pBuffer->pGraphics->DrawVertex3D(_pBuffer->DataA, _pBuffer->CurrentVertC, Graphics::PT_TRI_LIST);

	// reset
	memset(_pBuffer->DataA, 0, sizeof(Graphics::Vertex3D) * _pBuffer->MaxVertC);
    _pBuffer->CurrentVertC = 0;	
}
